﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace SGUI
{
    public class EventManager
    {
        Vector2 TouchPos;
        public void OnTouch(Vector2 pos)
        {
            //To UI Pos
            float sbp = UIRoot.Root.Size / Screen.height;
            pos *= sbp;
            pos.y -= UIRoot.Root.Size * 0.5f;
            pos.x -= (Screen.width * sbp) * 0.5f;
            TouchPos = pos;
            SGComponent targetNode = visitComponent(UIManager.RootNode);
            if (targetNode == null) 
            {
                return;
            }
            LinkedList<SGComponent> path = targetNode.GetPath(); 
            //捕获阶段
            this.EventCapture(path,targetNode);
            //目标阶段
             string OnClickFnName = (string)targetNode.GetProperty("OnClick").Value;
             if (OnClickFnName != "") 
             {
                 UIManager.JsEngine.CallGlobalFunction(OnClickFnName);
             }
            //冒泡阶段
            this.EventBubble(path,targetNode);
        }
        void EventCapture(LinkedList<SGComponent> lst,SGComponent targetNode) 
        {
            for (LinkedListNode<SGComponent> item = lst.Last; item != null;item = item.Previous)
            {
               Property useCapture =   item.Value.GetProperty("useCapture");
               if (useCapture != null && (bool)useCapture.Value) 
               {
                   
               }
            }
        }

        void EventBubble(LinkedList<SGComponent> lst,SGComponent targetNode) 
        {
            foreach (SGComponent item in lst)
            {
                 Property useCapture =   item.GetProperty("useCapture");
                 if (useCapture != null && !(bool)useCapture.Value)
                 {
                     
                 }
            }
        }
    
        SGComponent visitComponent(SGComponent parent)
        {
            if (parent.Children != null)
            {
                for (int i = parent.Children.Count-1; i >= 0; i--)
                {
                    SGComponent node = visitComponent(parent.Children[i]);
                    if (node != null) 
                    {
                        return node;
                    }
                }
            }
            if (this.IsInRect(parent, TouchPos)) 
            {
                return parent;
            }
            return null;
        }

        bool IsInRect(SGComponent node,Vector2 pos) 
        {
            if (!(node is UIElement)) 
            {
                return false;
            }
            UIElement elem = node as UIElement;
            Vector4 v4 = elem.GetElementSize();
            v4 *= (float)elem.PropertyValue<double>("Scale");
            v4.x += elem.WorldPos.x;
            v4.z += elem.WorldPos.x;
            v4.y += elem.WorldPos.y;
            v4.w += elem.WorldPos.y;
            if (pos.x >= v4.x && pos.x <= v4.z && pos.y > v4.y && pos.y < v4.w) 
            {
                return true;
            }
            return false;
        }
    }
}
